import re
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed

# 1. 配置请求头和目标链接列表
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'Connection': 'keep-alive',
    'Referer': 'https://gjzwfw.www.gov.cn/fwmh/login/change.do?redirectUrlKey=965d8899100d478fb1214e229fe986e7',
    'Cookie': '_dec_key=undefined; CUSSESSIONID=YTIxYzFkZWQtMjQzOS00MjkzLWE2NDAtOTJiYzdmZTc2N2E0; CUSSESSIONID_=MTUwNWQ0NDEtNGY3NS00ODcyLWI2MWQtMzY5MWY1MDJhZWU1; https_waf_cookie=ca7b7ea9-3b8c-420c13c57dd33e9c76a4e2f23a8d8ed4796e; _dec_key=undefined; wdcid=7bd24cec13880089; wdlast=1761812291; wdses=7de697adbb51a638; __asc=98ea8ea619a341c2dc84f907615; __auc=98ea8ea619a341c2dc84f907615; JSESSIONID=A1D5B654F724F0C3D940FC3573121B88',
    'Upgrade-Insecure-Requests': '1',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Priority': 'u=0, i',
}

url_list = [
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203813220167502452013134600030.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520300009500300T300073300300001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520303009501039B400073300300001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_1152030379528638X3452070913600003.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_1152030379528638X3452070913600009.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_1152030379528638X3452070913600013.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520321009515027T400073300300001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520322009518375Y452070913600003.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520322009518375Y452070913600011.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520322009518623L400073300300001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_1152032300952023XU452070913600011.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_1152032300952023XU452070913600013.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095224976400073300300003.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095227371452070913600001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095227371452070913600002.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095227371452070913600003.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095227371452070913600009.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095227371452070913600010.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095227371452070913600011.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095227371452070913600012.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520325009524054Y452070913600003.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520325009524054Y452070913600011.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520325009524353W400073300300004.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520326009526594C452070913600002.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520326009526594C452070913600010.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_1152032700952840XP452070913600011.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520330009536143X452070913600029.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203300095364859400073300300001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520381009510429G452070913600007.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520382009856235A452070913600003.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520382009856235A452070913600005.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520382009856235A452070913600012.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520382009856331K400073300300001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203000964557033400073300300001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203005771024150452070913600004.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203005771024150452070913600005.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095224976452080500300001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203023220298935452093100100001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520324337452117D452093100100001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203273373646002452093100100001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_1152032100951573XP452101700300001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520321009515051B452100901800002.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095227371452100901800001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520326009526594C452100901800002.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520330009536143X452100901800004.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520381009510429G452100901800001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520322551920073H452012200600009.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520303MB1563788M400052400300002.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520303MB1563788M452052402900001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520322MB15846180400052400100002.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520325MB1990712K400052400100006.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520303009501039B400073300300001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095224976400073300300003.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520325009524353W400073300300004.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_TE11520330009536XB452071514000002.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203000964557033400073300300001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520325009524353W452080500200002.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520326MB0R83605M452080500300002.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203213220565255452093101600001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520324337452117D452093100100001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203813220167502452093101600001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520300MB1845961L452093101600001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095223820452108600100002.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_115203240095223820452108600100005.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520328009530489E452108600100005.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520381009510816W452108600100001.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_11520381009510816W452108600100005.do',
'https://gjzwfw.www.gov.cn/fwmh/item/v3/item_1152038200985693XC452108600100001.do',
]

# 2. 定义单链接处理函数（核心逻辑）
def process_url(url):
    """处理单个URL：请求页面 + 提取咨询方式/监督投诉方式 + 检测异常"""
    result_data = {
        'url': url,
        'consult_info': '未提取到',  # 咨询方式
        'complaint_info': '未提取到',  # 监督投诉方式
        'is_abnormal': False,  # 是否异常（含"暂无"）
        'error': ''  # 错误信息（如请求失败）
    }

    try:
        # 发送请求（设置超时避免无限等待）
        response = requests.get(url=url, headers=headers, timeout=10)
        response.raise_for_status()  # 若状态码非200，抛出HTTPError

        # 定义正则表达式
        pattern_consult = r'<td width="195".*?>法定办结时限</td>.*?<td colspan="3">(.*?)</td>\s*</tr>'
        pattern_complaint = r'<td width="195".*?>承诺办结时限</td>.*?<td colspan="3">(.*?)</td>\s*</tr>'

        # 提取并处理“咨询方式”
        consult_result = re.findall(pattern_consult, response.text, re.DOTALL)
        if consult_result:
            result_data['consult_info'] = re.sub(r'\s+', ' ', consult_result[0]).strip()
            # 检测“暂无”异常
            if "日" not in result_data['consult_info']:
                result_data['is_abnormal'] = True

        # 提取并处理“监督投诉方式”
        complaint_result = re.findall(pattern_complaint, response.text, re.DOTALL)
        if complaint_result:
            result_data['complaint_info'] = re.sub(r'\s+', ' ', complaint_result[0]).strip()
            # 检测“暂无”异常
            if "日" not in result_data['complaint_info']:
                result_data['is_abnormal'] = True

    except Exception as e:
        # 捕获请求/解析错误（如超时、网络错误、正则无匹配）
        result_data['error'] = str(e)
        result_data['is_abnormal'] = True

    return result_data

# 3. 线程池批量处理（核心多线程逻辑）
if __name__ == '__main__':
    # 配置线程池：max_workers建议设为5-10（避免请求过于频繁被反爬）
    with ThreadPoolExecutor(max_workers=5) as executor:
        # 提交所有URL任务到线程池，返回任务对象列表
        future_tasks = {executor.submit(process_url, url): url for url in url_list}

        # 遍历已完成的任务，获取结果
        for future in as_completed(future_tasks):
            url = future_tasks[future]
            try:
                # 获取任务结果（若任务报错，此处会抛出异常）
                data = future.result()
                # 打印结果（可根据需求调整输出格式）
                print(f"\n=== 处理结果：{url} ===")
                print(f"咨询方式：{data['consult_info']}")
                print(f"监督投诉方式：{data['complaint_info']}")
                print(f"状态：{'异常' if data['is_abnormal'] else '正常'}")
                if data['error']:
                    print(f"错误信息：{data['error']}")
            except Exception as e:
                print(f"\n处理URL {url} 时发生未知错误：{str(e)}")